/**
 * 
 */
package com.kingre.et.action;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import javax.mail.MessagingException;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.kingre.et.model.School;
import com.kingre.et.model.User;
import com.kingre.et.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * @author Jingui Yu
 * 
 */
public class RegisterAction extends ActionSupport {
    private static final long serialVersionUID = -1902760154184184188L;
    private static final Logger logger = Logger.getLogger(RegisterAction.class);
    @Autowired
    private IUserService userService;
    private User user;
    private List<School> schoolList;
    private Integer subListId;

    @Override
    public String input() {
        schoolList = userService.findSchoolAll();
        logger.debug("schoolList =" + schoolList);

        return INPUT;
    }

    @Override
    public String execute() throws IOException, MessagingException {
        logger.debug("subListId=" + subListId);
        if (userService.usernameExist(user.getUsername())) {
            addFieldError("user.username", getText("user.username.exist"));
            return input();
        }
        if (userService.emailExist(user.getEmail())) {
            addFieldError("user.email", getText("user.email.exist"));
            return input();
        }
        School school = userService.getSchool(subListId);
        if (userService.studentIdExist(user.getStudentId(), school)) {
            addFieldError("user.studentId", getText("user.studentId.exist"));
            return input();
        }
        user.setSchool(school);
        user = userService.register(user);
        // userService.sendMail(
        // user.getEmail(),
        // getText("email.subject"),
        // getText("email.message", new String[]{
        // user.getName(), user.getId().toString(), user.getEmail()}));
        return SUCCESS;
    }

    @SuppressWarnings("unchecked")
    public List<School> getSubSchoolList() {
        ValueStack stack = ServletActionContext.getValueStack(ServletActionContext.getRequest());
        Object school = stack.findValue("top");
        if (school != null && school instanceof School) {
            return userService.findSubSchool(((School) school).getId());
        }
        return Collections.EMPTY_LIST;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<School> getSchoolList() {
        return schoolList;
    }

    public void setSchoolList(List<School> schoolList) {
        this.schoolList = schoolList;
    }

    public void setSubListId(Integer subListId) {
        this.subListId = subListId;
    }

}
